package TuiLayer;
import ControlLayer.EmployeeCtr;
import ModelLayer.Employee;
import java.util.Scanner;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class EmployeeUI
{
    // instance variables
    private EmployeeCtr employeeCtr;
    private Employee foundEmployee;

    /**
     * Constructor for objects of class EmployeeUI
     */
    public EmployeeUI()
    {
        employeeCtr = new EmployeeCtr();
    }

    public void createEmployee(String id, String name, String address, String phone, String email) 
    {
        employeeCtr.createEmployee(id, name, address, phone, email);
    }

    public Employee findEmployee(String id) 
    {
        return employeeCtr.findEmployee(id);
    }

    public void updateEmployee(Employee employee,String id, String name, String address, String phone, String email) 
    {
        employeeCtr.updateEmployee(employee,id, name, address, phone, email);
    }

    public void deleteEmployee(Employee employee) 
    {
        employeeCtr.deleteEmployee(employee);
    }

    private int getChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int choice = keyboard.nextInt();

        return choice;
    }

    // Employees menu
    public void menuStart() 
    {
        boolean exit = false;
        String print = null;
        while(!exit) 
        {
            writeEmployeesMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = employeeAddStart();
            }
            else if(choice == 2)
            {
                print = browseMenuStart();
            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
    }

    public String browseMenuStart() 
    {
        

        boolean exit = false;
        String print = null;
        print = employeeFindStart();
        while(!exit) 
        {
            writeBrowseEmployeesMenu(print);
            int choice = getChoice();

            if(choice == 1) 
            {
                print = employeeUpdateStart();
            }
            else if(choice == 2)
            {
                print = employeeRemoveStart();

            }
            else if(choice == 0) 
            {
                exit = true;
            }

        }
        return print;
    }

    // Write employee menu
    public void writeEmployeesMenu(String print) 
    {
        System.out.println("\f *** Employees ***");
        System.out.println(" (1)Add");
        System.out.println(" (2)Find / Edit");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    public void writeBrowseEmployeesMenu(String print) 
    {
        System.out.println("\f *** Employee ID: " + foundEmployee.getId() + " ***");
        System.out.println(" (1)Edit");
        System.out.println(" (2)Delete");
        System.out.println(" (0)Back");
        if(print != null) 
        {
            System.out.println("\n " + print);

        }
        System.out.print("\n Make your choice: ");
    }

    // Start "Add a employee"
    public String employeeAddStart() 
    {
        String print = null;
        System.out.println("\nEnter ID: ");
        Scanner sc = new Scanner(System.in);
        String id = sc.nextLine();

        System.out.println("Enter name: ");
        String name = sc.nextLine();

        System.out.println("Enter address: ");
        String address = sc.nextLine();

        System.out.println("Enter phone: ");
        String phone = sc.nextLine();

        System.out.println("Enter email: ");
        String email = sc.nextLine();

        Employee employee = employeeCtr.findEmployee(id);
        
        if(employee == null) 
        {
            createEmployee(id,name,address,phone,email);

            print = "Employee #" + id + " has been added to your Employees.";
        }
        else 
        {
            print = "ERROR: Employee already exists.";
        }

        return print;
    }

    // Start "Find a employee"
    public String employeeFindStart() 
    {
        String print = null;
        System.out.print("\nEnter ID: ");

        Scanner sc = new Scanner(System.in);
        String id = sc.next();

        foundEmployee = findEmployee(id);
        if(foundEmployee != null) 
        {
            print = " # Found Employee: #" + "\n" 
            + "  ID: " + foundEmployee.getId() + "\n"
            + "  Name: " + foundEmployee.getName() + "\n"
            + "  Address: " + foundEmployee.getAddress() + "\n"
            + "  Phone: " + foundEmployee.getPhone() + "\n"
            + "  E-mail: " + foundEmployee.getEmail() + "\n";
        }
        else 
        {
            print = "ERROR: Employee not found.";
        }
        return print;
    }

    // Start "Update a employee"
    public String employeeUpdateStart() 
    {
        String print = null;
        System.out.println("\nEnter new ID: ");
        Scanner sc = new Scanner(System.in);
        String newId = sc.nextLine();

        System.out.println("Enter new name: ");
        String newName = sc.nextLine();

        System.out.println("Enter new address: ");
        String newAddress = sc.nextLine();

        System.out.println("Enter new phone: ");
        String newPhone = sc.nextLine();

        System.out.println("Enter new email: ");
        String newEmail = sc.nextLine();

        updateEmployee(foundEmployee,newId,newName,newAddress,newPhone,newEmail);

        print = "Employee #" + newId + " has been updated to:\n" + getEmployeeInfo(foundEmployee);

        return print;
    }

    private String getEmployeeInfo(Employee employee) 
    {
        String string = "  ID: " + employee.getId() + "\n"
            + "  Name: " + employee.getName() + "\n"
            + "  Address: " + employee.getAddress() + "\n"
            + "  Phone: " + employee.getPhone() + "\n"
            + "  Email: " + employee.getEmail() + "\n";
        return string;
    }

    // Start "Remove a employee"
    public String employeeRemoveStart() 
    {
        String print = null;

        deleteEmployee(foundEmployee);
        print = "Employee #" + foundEmployee.getId() + " has been deleted.";
        
        return print;
    }
}
